<template lang="pug">
mt-dialog(
  v-model="appmodel"
  cancel-text="取消"
  title="作废"
  :on-close="updateModel"
  :on-cancel="updateModel"
  :on-confirm="updateConfirmModel"
  size="small"

)
  h3 确认后不可撤回 请谨慎操作！
    mt-table(
      v-bind="tableProps"
      style="margin-top: 10px;"
    )
    mt-form(
        v-bind="editFormProps"
        style="margin-top: 10px;"
      )
  </template>

<script setup lang="ts">
import { useApi } from "@scmt/core-api";
import {
  type FormItemProps,
  type TableProps,
  useNotify,
  type EditFormProps,
} from "@scmt/common-components";
import { type SpecimenEntity } from "@scmt/api-lis-specimen";
import { useRequest } from "@scmt/common-hooks";
import { ESpecimenStatus } from "@scmt/api-common";
//添加 Props属性
const props = defineProps({
  selectionRow: {
    type: Array as PropType<SpecimenEntity[]>,
    default: () => [],
  },
});
const $api = useApi();
const $notify = useNotify();
const editingObj = ref<{} | Object>({});

const appmodel = defineModel({
  default: false,
});
const emit = defineEmits(["message-to-parent"]);
const updateModel = () => {
  appmodel.value = false;
};

const { request, loadingMap } = useRequest({
  save: (q) => {
    return $api.lis.specimen.batchGenerateBarcode(q).then(() => {
      $notify.success("作废成功");
      emit("message-to-parent");
      appmodel.value = false;
    });
  },
});

const updateConfirmModel = () => {
  if (!editingObj?.value?.reason) {
    $notify.error("请选择作废原因");
    return;
  }
  const specimenIdData = []
    .concat(props.selectionRow)
    .map((item) => {
      return item.id;
    })
    .filter((item) => item != undefined);
  request("save", {
    specimenList: specimenIdData,
    status: ESpecimenStatus.Rejection,
  });
};

const editFormProps = computed<EditFormProps>(() => {
  const formItems: FormItemProps[] = [
    {
      prop: "reason",
      label: "原因:",
      required: true,
      as: "select",
      options: {
        loadMethod: () => {
          return [
            {
              label: "错过执行事件",
              value: 1,
            },
            {
              label: "患者拒绝",
              value: 2,
            },
            {
              label: "院外执行",
              value: 3,
            },
            {
              label: "其他",
              value: 4,
            },
          ];
        },
      },
    },
    {
      prop: "remark",
      label: "备注:",
      required: false,
      options: {
        type: "textarea",
      },
    },
  ];

  const editProps: EditFormProps = {
    items: formItems,
    model: editingObj.value,
    columns: 1,
  };
  return editProps;
});

const tableProps = computed(() => {
  const tableProps: TableProps = {
    data: props.selectionRow.filter((item) => item.id != undefined),
    columns: [
      {
        prop: "patientName",
        label: "姓名",
        width: 120,
      },
      {
        prop: "sex",
        label: "性别",
      },
      {
        prop: "age",
        label: "年龄",
      },
      {
        prop: "applicationProject",
        label: "申请项目",
      },
    ],
  };
  return tableProps;
});
watch(
  appmodel,
  (visible: boolean) => {
    if (visible) {
      editingObj.value = {};
    }
  },
  {
    immediate: true,
  }
);
</script>
